using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public enum BirdState
{
    Waiting,
    BeforeShoot,
    AfterShoot,
    WaitToDie
}

public class Bird : MonoBehaviour
{
    //三个状态 等待  发射前  发射后
    public BirdState state = BirdState.BeforeShoot;

    [SerializeField] private float maxDistance = 2;
    [SerializeField] private float flySpeed = 15;

    protected Rigidbody2D rgb;

    private bool isMouseDown = false;
    private bool isFlying = true;
    private bool isHaveUsedSkill = false;

    private void Start()
    {
        rgb = GetComponent<Rigidbody2D>();
        rgb.bodyType = RigidbodyType2D.Static;
    }

    private void Update()
    {
        switch (state)
        {
            case BirdState.Waiting:
                WaitControll();
                break;
            case BirdState.BeforeShoot:
                MoveControll();
                break;
            case BirdState.AfterShoot:
                StopControl();
                SkillControl();
                break;
            case BirdState.WaitToDie:
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
    //鼠标事件OnMouseDown OnMouseUp这些API使用需要确保目标物体身上最好有Collider

    private void OnMouseDown()
    {
        if (state == BirdState.BeforeShoot && EventSystem.current.IsPointerOverGameObject() == false)
        {
            isMouseDown = true;
            Slingshot.Instance.StartDraw(transform);
            AudioManager.Instance.PlayBirdSelect(transform.position);
        }
    }

    private void OnMouseUp()
    {
        if (state == BirdState.BeforeShoot && EventSystem.current.IsPointerOverGameObject() == false)
        {
            isMouseDown = false;
            Slingshot.Instance.EndDraw();
            Fly();
        }
    }

    private void WaitControll()
    {
        
    }

    private void MoveControll()
    {
        if (isMouseDown)
        {
            transform.position = GetMousePosition();
        }
    }

    private Vector3 GetMousePosition()
    {
        Vector3 centerPosition = Slingshot.Instance.getCenterPosition();
        Vector3 mp = Camera.main.ScreenToWorldPoint(Input.mousePosition); //将屏幕坐标转换为世界坐标
        mp.z = 0; //因为上面的坐标是相机的平面坐标所以z会发生改变
        Vector3 mouseDir = mp - centerPosition; //空间换时间

        float distance = mouseDir.magnitude; //计算鼠标点击位置到中心点的距离

        if (distance > maxDistance)
        {
            mp = mouseDir.normalized * maxDistance + centerPosition; //对鼠标控制皮筋的绘制进行长度的限制超过某个长度则限制在范围内
        }

        return mp;
    }

    private void Fly()
    {
        rgb.bodyType = RigidbodyType2D.Dynamic;
        rgb.velocity = (Slingshot.Instance.getCenterPosition() - transform.position).normalized * flySpeed;
        state = BirdState.AfterShoot;
        AudioManager.Instance.PlayBirdFlying(transform.position);
    }

    public void GoStage(Vector3 position)
    {
        state = BirdState.BeforeShoot;
        transform.position = position;
    }

    private void StopControl()
    {
        if (rgb.velocity.magnitude < 0.1f)
        {
            state = BirdState.WaitToDie;
            Invoke("LoadNexBird", 1f);
        }
    }

    protected void LoadNexBird()
    {
        Destroy(gameObject);
        GameObject.Instantiate(Resources.Load("Boom1"), transform.position, Quaternion.identity);
        GameManager.Instance.LoadNextBird();
    }

    private void OnCollisionEnter2D(Collision2D other)
    {
        if (state == BirdState.AfterShoot)
        {
            isFlying = false;
        }
        if (state == BirdState.AfterShoot && other.relativeVelocity.magnitude > 5)
        {
            AudioManager.Instance.PlayBirdCollision(transform.position);
        }
    }

    private void SkillControl()
    {
        if (isHaveUsedSkill)
        {
            return;
        }
        if (Input.GetMouseButtonDown(0) && isFlying == true)
        {
            isHaveUsedSkill = true;
            FlyingSkill();
        }

        if (Input.GetMouseButtonDown(0))
        {
            isHaveUsedSkill = true;
            FullTimeSkill();
        }
    }

    protected virtual void FlyingSkill()
    {
    }

    protected virtual void FullTimeSkill()
    {
    }
}